<template>
  <div class="productList">
    <van-nav-bar
      title="进口狗粮"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <template #right><van-icon name="search" size="18" /></template>
    </van-nav-bar>
    <div class="sort">
      <span :class="{ active: active == 1 }" @click="change(0)">综合</span>
      <span :class="{ active: active == 0 }" @click="change(1)">价格</span>
    </div>

    <ul class="sort_content clearfix">
      <li
        class="clone"
        v-for="(item, index) in arr"
        :key="index"
        @click="goClick(item._id)"
      >
        <div class="clone_img">
          <van-image :src="item.imgurl" fit="scale-down" />
        </div>
        <p class="clone_title">{{ item.title }}</p>
        <p class="clone_msg">{{ item.littleTitle }}</p>
        <p class="clone_price">¥{{ item.price }}</p>
      </li>
    </ul>
    <div class="classify_footer"></div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import http from "../utils/api";
import router from "@/router/index.js";
export default {
  setup() {
    const active = ref(1);
    const arr = ref();
    onMounted(() => {
      http.getProductApi().then((res) => {
        console.log(res.data);
        arr.value = res.data.data;
        console.log(arr.value);
      });
    });
    const goClick = ref(function (_id) {
      // console.log(_id);
      router.push("/product/" + _id);
    });
    return {
      active,
      arr,
      goClick,
    };
  },
  methods: {
    onClickLeft() {
      router.go(-1);
    },
    onClickRight() {},
    change(num) {
      if (num == 1) {
        this.active = 0;
      } else {
        this.active = 1;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.productList {
  width: 100%;
  .sort {
    width: 100%;
    height: 35px;
    padding: 25px;
    background-color: #fff;
    color: grey;
    font-size: 14px;
    padding: 0 50px;
    span {
      margin-right: 25px;
      line-height: 35px;
    }
    .active {
      color: black;
    }
  }
  .sort_content {
    width: 100%;
    .clone {
      width: 176px;
      margin: 5px;
      float: left;
      background-color: #fff;
      .clone_img {
        width: 176px;
        height: 180px;
      }
      .van-image {
        width: 176px;
        height: 180px;
      }
      .clone_title {
        font-size: 15px;
        text-align: center;
        line-height: 21px;
        padding: 0 10px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .clone_msg {
        font-size: 14px;
        color: grey;
        line-height: 19px;
        padding: 0 10px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }
      .clone_price {
        font-size: 13px;
        color: red;
        margin: 10px;
      }
    }
  }
}
</style>
